package org.ariadne_eu.hcifetcher.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class AddStudentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AddStudentServlet.class
			.getName());
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String name = req.getParameter("name");
		String scoreString = req.getParameter("score");
		int score = 0;
		if(scoreString != null)
			score = Integer.parseInt(scoreString);
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Entity student = new Entity("Student");
		student.setProperty("name", name);
		student.setProperty("score", score);
		student.setProperty("datestamp", new Date());
		Key k = datastore.put(student);
		resp.getWriter().write("Added student " + name + " with score: " + score + ". Key: " + k);
		log.log(Level.INFO, "Added student " + name + " with score: " + score + ". Key: " + k);
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req,resp);
	}


}
